# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
脚本类，用来对组合好的参数进行循环执行，依次带入组合参数请求。可加入对响应结果断言） 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import time

import pymysql
import requests

from logger import Logger
from parameter.qdzjmsloanCs import parameter

logger = Logger("qdzjmsloan").get_log()

parameter = parameter()

url = 'http://test.coo.msxiaodai.com/msloan/api'   #'http://39.108.104.213:800/msloan/api'  # 测试环境
#url = 'http://39.108.104.213:803/msloan/api'    # 开发环境
headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
            'content-Type': 'application/json'
        }

logger.info('-----------渠道资金系统接口测试开始---------')

def jinjian():
    logger.info('-----------进件接口字段校验测试开始---------')
    data_list = parameter.get_jinjian_data('../files/qdzjmsloan.xls')     # 测试必填字段缺失
    result = []
    for data in data_list:
        #print(data)
        logger.debug('进件接口上送报文信息：%s',data)
        status = data[1]
        req1 = requests.post(url, json=data[0], headers=headers)
        fhcode = str(req1.status_code)
        if fhcode == '200':
            logger.debug('进件接口返回报文信息：%s', req1.text)
            respond = eval(req1.text)['serviceInfo']['serviceHeader']['servResponse']['code']
            # print(respond)
            if respond != status:
                logger.info("失败案例的进件信息：%s", data[0])
                logger.info("失败案例的响应信息：%s", req1.text)
                result.append('false')
            else:
                result.append('true')
        else:
            logger.info("http error info：%s", fhcode)
    #print(result)
    return result


def qdzj_report():
    logger.info('-----------报表测试进件接口测试开始---------')
    data_list = parameter.get_report_data('../files/qdzjmsloan.xls')    # 测试报表
    result = []
    for data in data_list:
        logger.debug('报表测试进件接口上送报文信息：%s', data)
        status = data[1]
        req1 = requests.post(url, json=data[0], headers=headers)
        fhcode = str(req1.status_code)
        if fhcode == '200':
            logger.debug('报表测试进件接口返回报文信息：%s', req1.text)
            respond = eval(req1.text)['serviceInfo']['serviceHeader']['servResponse']['code']
            # print(respond)
            if respond != status:
                logger.info("失败案例的进件信息：%s", data[0])
                logger.info("失败案例的响应信息：%s", req1.text)
                result.append('false')
            else:
                result.append('true')
        else:
            logger.info("http error info：%s", fhcode)
    #print(result)
    return result


def qdzj_approval():
    logger.info('-----------形式审批测试进件接口测试开始---------')
    data_list = parameter.get_approval_data('../files/qdzjmsloan.xls')    # 测试报表
    result = []
    for data in data_list:
        logger.debug('形式审批测试进件接口上送报文信息：%s', data)
        row = []
        caseId = data[0]           # 进件ID
        approval_status = data[1]  # 审批状态
        message = data[2]          # 进件报文
        status = data[3]           # 进件状态
        req1 = requests.post(url, json=message, headers=headers)
        fhcode = str(req1.status_code)
        if fhcode == '200':
            logger.debug('形式审批测试进件接口返回报文信息：%s', req1.text)
            respond = eval(req1.text)['serviceInfo']['serviceHeader']['servResponse']['code']
            # print(respond)
            if respond != status:
                logger.info("失败案例的进件信息：%s", message)
                logger.info("失败案例的响应信息：%s", req1.text)
            else:
                row.append(caseId)
                row.append(approval_status)
                result.append(row)
        else:
            logger.info("http error info：%s", fhcode)
    #print(result)
    return result


def approval_status():
    db = pymysql.connect(
        host="10.16.20.182",  # 测试环境
        # host="10.16.20.190",     # 开发环境
        user="dev",  # 测试：dev   开发：root
        password="dev",  # 测试：dev   开发：123456
        port=3306,
        use_unicode=True,
        charset="utf8",
        database="qdzjmsloandb")
    cursor = db.cursor()
    data_list = qdzj_approval()
    time.sleep(60)
    logger.info('-----------形式审批结果验证测试开始---------')
    result = []
    for data in iter(data_list):
        logger.debug('形式审批预期结果验证信息：%s', data)
        caseId = data[0]  # 进件ID
        approval_status = data[1]  # 审批状态
        sql = "select status from process_main where channel_process_no = '{0}';".format(caseId)
        cursor.execute(sql)
        results = cursor.fetchone()
        logger.debug('形式审批实际结果验证信息：%s', results[0])
        if approval_status !=  results[0]:
            logger.info("审批错误的进件ID：%s",caseId)
            result.append('false')
        else:
            result.append('true')
    db.close()
    return result


def inquiry():
    logger.info('-----------客户状态查询接口测试开始---------')
    data_list = parameter.get_inquiry_data('../files/qdzjmsloan.xls')     # 测试必填字段缺失
    result = []
    for data in data_list:
        logger.debug('客户状态查询接口上送报文信息：%s', data)
        inquiry_result = data[1]
        status = data[2]
        req1 = requests.post(url, json=data[0], headers=headers)
        fhcode = str(req1.status_code)
        if fhcode == '200':
            logger.debug('客户状态查询接口返回报文信息：%s', req1.text)
            inquiry = eval(req1.text)['serviceInfo']['serviceBody']['response']['result']
            respond = eval(req1.text)['serviceInfo']['serviceHeader']['servResponse']['code']
            # print(respond)
            if respond != status or inquiry != inquiry_result :
                logger.info("失败案例的进件信息：%s", data[0])
                logger.info("失败案例的响应信息：%s", req1.text)
                result.append('false')
            else:
                result.append('true')
        else:
            logger.info("http error info：%s", fhcode)
    #print(result)
    return result

if __name__ == '__main__':
    qdzj_report()